package com.yzc.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yzc.db.SpringDB;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserBeanServiceImpl extends ServiceImpl<UserBeanMapper, UserBean> {

    final UserBeanMapper userBeanMapper;
    final SpringDB springDB;

    public Map login(String name, String pass) {
        Map map = new HashMap();
        //访问数据库
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        queryWrapper.eq("pass", pass);
        List<UserBean> list = userBeanMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            UserBean userBean = list.get(0);
            map.put("username", userBean.getName());
            map.put("userid", userBean.getId());
            map.put("result", "success");
        } else {
            map.put("result", "error");
        }
        return map;
    }

    public List<UserBean> loadaAll() {
        //操作
        return userBeanMapper.loadaAll();
    }

    public Map updateInfo(UserBean userBean) {
        Map map = new HashMap();

        try {
            userBeanMapper.updateById(userBean);
            userBeanMapper.deleteRolesByUserId(userBean.getId());
            int userid = userBean.getId();
            int[] roleids = userBean.getRoleids();
            for (int i = 0; i < roleids.length; i++) {
                String sql = "insert into t_user_kz values(0," + userid + "," + roleids[i] + ",0)";
                springDB.getJdbcTemplate().execute(sql);
            }
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }

    public Map saveInfos(UserBean userBean) {

        Map map = new HashMap();

        try {
            save(userBean);
            int userid = userBean.getId();
            int[] roleids = userBean.getRoleids();
            for (int i = 0; i < roleids.length; i++) {
                String sql = "insert into t_user_kz values(0," + userid + "," + roleids[i] + ",0)";
                springDB.getJdbcTemplate().execute(sql);
            }
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;

    }

    public List selectInfos(String name) {
        return userBeanMapper.selectInfosByWhere(name);
    }

    public UserBean getInfoById(String id) {
        return userBeanMapper.selectById(id);
    }

    public Map deleteInfo(UserBean userBean) {

        Map map = new HashMap();

        try {
            userBeanMapper.deleteById(userBean);
            userBeanMapper.deleteRolesByUserId(userBean.getId());
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }


    /*public UserBean getuserid(int id) {

        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        UserBean userid = userBeanMapper.selectOne(queryWrapper);
        return userid;
    }*/

    public Map updatePass(UserBean userBean) {
        Map map = new HashMap();

        try {
            userBeanMapper.updatePass(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }

    public Map updateXinxi(UserBean userBean) {
        Map map = new HashMap();

        try {
            userBeanMapper.updateXinxi(userBean);
            map.put("result", "success");
        } catch (Exception e) {
            map.put("result", "error");
            throw new RuntimeException(e);
        }
        return map;
    }
}




